package cn.com.huzo.webapp.controller.master;

import huzofw.common.web.controller.BaseFormController;
import cn.com.huzo.model.entities.master.MpsItemEntity;
import cn.com.huzo.model.entities.master.MpsItemUsageRuleEntity;
import cn.com.huzo.service.master.MpsItemManager;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 主产品计划项目控制器
 *
 * @author 吴旭
 */
@Controller
public class MpsItemController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    @Resource(name = "MpsItemManager")
    private MpsItemManager _manager;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到主产品计划项目一览画面
     *
     * @return
     */
    @RequestMapping("master/mps_item/list.html")
    public ModelAndView redirectListWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("master.mps_item.list.def");
        return view;
    }

    /**
     * 弹出主产品计划项目选择画面
     * <p>
     * @return
     */
    @RequestMapping("master/mps_item/choice.html")
    public ModelAndView redirectChoicePopupWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.master.mps_item.choice.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="数据处理部分">
    /**
     * 得到全部主产品计划项目数据列表
     *
     * @return 全部主产品计划项目数据列表
     * @throws java.lang.Exception
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "master/mps_item/fetchAll.html",
            method = RequestMethod.POST,
            produces = {"application/xml", "application/json"})
    public @ResponseBody
    List<MpsItemEntity> fetchAll() throws Exception {
        return _manager.fetchAll();
    }

    /**
     * 得到分配给该用户的计划跟踪项目
     *
     * @param userId 用户ID
     * @return 该用户的计划跟踪项目
     * @throws java.lang.Exception
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "master/mps_item/fetchAllByUser.html",
            method = RequestMethod.POST,
            produces = {"application/xml", "application/json"})
    public @ResponseBody
    List<MpsItemEntity> fetchAllByUser(@RequestBody final Long userId) throws Exception {
        return _manager.fetchAllByUser(userId);
    }

    /**
     * 批量增加或更新主产品计划项目。
     *
     * @param criterion 要处理的数据列表
     * @return
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/mps_item/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> save(@RequestBody final Map<String, List<MpsItemEntity>> criterion) throws Exception {
        return _manager.saveLines(criterion);
    }

    /**
     * 删除。
     *
     * @param criterion 要删除的数据
     * <p>
     * @return
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/mps_item/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager.delete(criterion);
    }

    /**
     * 更新排列顺序。
     *
     * @param itemList 要更新的数据
     * @return 影响行数
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/mps_item/updateLinesOrder.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer updateLinesOrder(@RequestBody final List<MpsItemEntity> itemList) throws Exception {
        return _manager.updateLinesOrder(itemList);
    }

    /**
     * 授权。
     *
     * @param itemList 授权数据
     * @return 影响行数
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/mps_item/grant.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer grant(@RequestBody final List<MpsItemUsageRuleEntity> itemList) throws Exception {
        return _manager.grant(itemList);
    }
    
    /**
     * 回收权限。
     *
     * @param userId 用户ID
     * @return 影响行数
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/mps_item/revoke.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer revoke(@RequestBody final Long userId) throws Exception {
        return _manager.revoke(userId);
    }
    // </editor-fold>
}
